package com.hnxxxy.sysmgr.service.impl;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.sysmgr.mapper.ISysDictionaryMapper;
import com.hnxxxy.sysmgr.pojo.Dict;
import com.hnxxxy.sysmgr.service.SysDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Slf4j
public class SysDictionaryServiceImpl implements SysDictionaryService {

    @Autowired
    ISysDictionaryMapper iSysDictionaryMapper;

    @Override
    public List<Dict> findAlSysDictionary(Dict dict) {
        return iSysDictionaryMapper.findAlSysDictionary(dict);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteSysDictionary(Integer[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                iSysDictionaryMapper.deleteSysDictionary(ids[i]);
            }
            return AjaxRtnEntity.success;
        } catch (Exception e) {
            log.info("-->字典管理删除异常：" + e.toString());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxRtnEntity.error;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateSysDictionary(Dict dict) {
        try {
            iSysDictionaryMapper.updateSysDictionary(dict);
            return AjaxRtnEntity.success;
        } catch (Exception e) {
            log.info("-->字典管理修改异常：" + e.toString());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxRtnEntity.error;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertSysDictionary(Dict dict) {
        try {
            iSysDictionaryMapper.insertSysDictionary(dict);
            return AjaxRtnEntity.success;
        } catch (Exception e) {
            log.info("-->字典管理添加异常：" + e.toString());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxRtnEntity.error;
        }
    }
}
